﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoFix.Base;
using BusinessLogicLayer.Manager;
using Common;
using Objects;
using System.Text;

namespace AutoFix
{
    public partial class step_list : BasePage
    {
        #region [BasePage Implementations]
        /// <summary>
        /// Gets or sets a value indicating whether [is login page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is login page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsLoginPage { get { return false; } }

        /// <summary>
        /// Gets or sets a value indicating whether [is anonymous page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is anonymous page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsAnonymousPage { get { return false; } }

        /// <summary>
        /// Page name
        /// </summary>
        protected override string PageName
        {
            get { return "Step List"; }
        }

        /// <summary>
        /// Gets the authorized user types.
        /// </summary>
        protected override List<UserType> AuthorizedUserTypes
        {
            get { return new List<UserType>(new UserType[] { UserType.Admin, UserType.Contributor }); }
        }

        #endregion

        #region [Event Handlers]
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Page.IsPostBack))
            {
                if (this.PageSession.Article != null)
                {
                    int articleID = this.PageSession.Article.ArticleID;
                    if (articleID > 0)
                    {
                        StepManager stepManager = new StepManager();
                        List<GetStepsUserOrArticle> steps = null;
                        ArticleManager articleManager = new ArticleManager();
                        try
                        {
                            this.PageSession.Article = articleManager.GetArticles(articleID)[0];
                            steps = stepManager.GetStepsByArticleID(articleID);
                            if (steps != null && steps.Count > 0)
                            {
                                foreach (GetStepsUserOrArticle step in steps)
                                {
                                    if (this.PageSession.Article.ArticleTypeID == (int)ArticleEnums.Blog)
                                    {
                                        StringBuilder pathToFile = new StringBuilder();
                                        pathToFile.Append(AppConfiguration.ArticlePath);
                                        pathToFile.Append("/");
                                        pathToFile.Append(articleID);
                                        pathToFile.Append("/");
                                        pathToFile.Append(step.StepURL);
                                        string completePath = Server.MapPath(pathToFile.ToString());
                                        step.StepContent = LoadStepData(completePath);
                                        pathToFile = null;
                                    }

                                    step.IsPublished = !step.IsPublished;
                                }
                                this.dtlSteps.DataSource = steps;
                                this.dtlSteps.DataBind();
                            }
                            else
                            {
                                this.pageActionMessage.InnerText = "There are no steps for this article.";
                                this.pageActionMessage.Attributes.Add("class", "information-msg");
                            }

                        }
                        catch (Exception)
                        {
                            this.pageActionMessage.InnerText = "Failed to load step list.";
                            this.pageActionMessage.Attributes.Add("class", "error-msg");
                        }
                        finally
                        {
                            stepManager = null;
                            steps = null;
                            articleManager = null;
                        }
                    }
                }
            }


        } 
        #endregion

        #region [Private Methods]
        /// <summary>
        /// Reads the loaded  data
        /// </summary>
        /// <param name="stepURL">the step url</param>
        /// <returns>the lines in the text</returns>
        private string LoadStepData(string stepURL)
        {

            string lines = System.IO.File.ReadAllText(stepURL);
            return lines;

        }
        #endregion
    }
}